// webpack.config.prod.js
const webpack=require('webpack')
const base = require('./webpack.config')
const childProcess = require('child_process')

const cmd = (cmdStr) => {
    return new Promise((resolve) => {
      childProcess.exec(cmdStr,{
        maxBuffer: 1024 * 2000
      }, function(err, stdout, stderr) {
          if (err) {
              console.log(err);
          } else if (stderr.lenght > 0) {
              console.error(stderr.join('\n'))
          } else {
              console.log(stdout);
          }
  
          resolve()
      });
    })
}

module.exports = Object.assign({}, base, {
    mode: "production",
    // 不属于内部的库, 外部的
    externals: {
        react: {
            commonjs: 'react',
            commonjs2: 'react',
            amd: 'react',
            root: 'React'
        },
        'react-dom': {
            commonjs: 'react-dom',
            commonjs2: 'react-dom',
            amd: 'react-dom',
            root: 'ReactDOM'
        }
    },
    plugins: [new webpack.ProgressPlugin(function handler(percentage, msg) {
        if (percentage == 0) {
            console.log('开始编译');
        }

        if (percentage == 1) {
            console.log('结束编译');
            cmd('yalc push').then(() =>{
                console.log("yalc push 成功");
            })
        }
    })],
});